package com.sr.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sr.dao.AuthorDaoImp;
import com.sr.entity.Author;
import com.sr.service.AuthorService;

/**
 * Servlet implementation class AuthorRegisterServlet
 */
@WebServlet("/doregister")
public class AuthorRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AuthorRegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		String username=request.getParameter("username");
		String penname=request.getParameter("authorpenname");
		String idcard=request.getParameter("idcard");
		String password=request.getParameter("password");
		
		Author author=new Author();
		author.setUserName(username);
		author.setPassword(password);
		author.setIdCard(idcard);
		author.setAuthorPenName(penname);
		if(username==null || username.equals("")){
			System.out.println("用户名不能为空！");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}else if(password==null || password.equals("")){
			System.out.println("密码不能为空！");
			request.getRequestDispatcher("authorregister.jsp").forward(request, response);
		}else if(idcard==null || idcard.equals("")){
			System.out.println("身份证不能为空！");
			request.getRequestDispatcher("authorregister.jsp").forward(request, response);
		}else if(penname==null || penname.equals("")){
			System.out.println("笔名不能为空！");
			request.getRequestDispatcher("authorregister.jsp").forward(request, response);
		}else{
			AuthorService as=new AuthorService();
			boolean result=as.addAuthor(author);
			if(result){
				response.sendRedirect("registersuccess.jsp");
			}else{
				System.out.println("注册失败！");
				response.sendRedirect("authorregister.jsp");
			}
		}
		
	}
	

}
